﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Random = UnityEngine.Random;

public static class StringTool
{
    public enum bType
    {
        Int,
        Float,
        String,
    }

    public static bType toType(this string s)
    {
        int d = 0;
        bool isint = true;
        for (int i = 0; i < s.Length; i++)
        {
            if ((s[i] >= '0' && s[i] <= '9') == false)
            {
                if (s[i] == '.' && d == 0)
                {
                    d++;
                }
                else
                {
                    isint = false;
                    break;
                }
            }
        }
        if (d == 1 && isint)
        {
            return bType.Float;
        }
        else if (d == 0 && isint)
        {
            return bType.Int;
        }
        else
        {
            return bType.String;
        }
    }

    public static T tolog<T>(this T obj, bool isdebug = true)
    {
        debug.log(isdebug, obj);
        return obj;
    }

    /// <summary>
    /// 显示类型的状态值为二进制显示
    /// </summary>
    public static string to2s(this int num)
    {
        return Convert.ToString(num, 2);
    }

    private static StringBuilder sb = new StringBuilder();

    /// <summary>
    /// 字符串转int
    /// </summary>
    /// <param name="s">字符串</param>
    /// <returns></returns>
    public static int toInt(this string s)
    {
        int v = -1;
        if (s == null)
        {
            return -1;
        }
        //try
        //{
        if (s.Length > 0 && ((s[0] >= '0' && s[0] <= '9') || s[0] == '-'))
        {
            v = int.Parse(s);
        }
        else
        {
            sb.Remove(0, sb.Length);
            for (int i = 0; i < s.Length; i++)
            {
                if ((s[i] >= '0' && s[i] <= '9') || s[i] == '-')
                {
                    sb.Append(s[i]);
                }
            }
            if (sb.Length <= 0)
            {
                debug.log("转换 int 解析失败!!  ", s);
                v = -1;
            }
            else
            {
                v = int.Parse(sb.ToString());
            }
        }
        //}
        //catch (System.Exception ex)
        //{
        //    debug.log(debug.Level.error_3, "转换 int  数值出错 !!!!", s, ex.ToString());
        //}
        return v;
    }

    public static List<int> toInt(this List<float> s)
    {
        List<int> v = new List<int>();
        for (int i = 0; i < s.Count; i++)
        {
            v.Add(s[i].toInt());
        }
        return v;
    }

    public static float toFloat(this string s)
    {
        float v = -1;
        try
        {
            v = float.Parse(s);
        }
        catch (System.Exception ex)
        {
            debug.log(debug.Level.error_3, "转换 float 错误!!!!", s, ex.ToString());
        }
        return v;
    }

    public static string toString(this string s)
    {
        return s;
    }

    public static T toEnum<T>(this string eunm)
    {
        T t = default(T);
        try
        {
            t = (T)(Enum.Parse(typeof(T), eunm));
        }
        catch (System.Exception)
        {
            debug.log(debug.Level.error_3, "字符串转枚举类似错误!!", eunm, typeof(T));
        }
        return t;
    }

    public static List<string> toGetPinying(this string s)
    {
        List<string> vs = new List<string>();
        foreach (char vChar in s)
        {
            if ((int)vChar >= 19968 && (int)vChar <= 40869)
            {
                vs.Add(vChar + "");
            }
        }
        return vs;
    }

    /// <summary>
    /// 字符串转intList
    /// </summary>
    public static int[] toIntArr(this string s, char gap = '|')
    {
        string[] satt = s.toSplit(gap);
        int item = 0;
        int[] list = new int[satt.Length];
        for (int i = 0; i < satt.Length; i++)
        {
            try
            {
                item = int.Parse(satt[i]);
            }
            catch (System.Exception ex)
            {
                debug.log(debug.Level.error_3, ex.ToString());
            }
            list[i] = item;
        }
        return list;
    }

    //转换字符串并且保留N位小数
    public static string toString(this float v, int len = 2)
    {
        string vs = string.Empty;
        v = v.toRound(len);
        if (len == 0)
        {
            vs = ((int)v).toString();
        }
        else
        {
            vs = string.Format(string.Format("{0}0:F{1}{2}", "{", len, "}"), v);
        }

        return vs;
    }

    /// <summary>
    /// 界面数字字符串,需要正负符号
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static string toUIString(this int num)
    {
        if (num >= 0)
        {
            return string.Format("+{0}", num);
        }
        else
        {
            return num.toString();
        }
    }

    public static string toUIString(this float num)
    {
        if (num >= 0)
        {
            return string.Format("+{0}", num);
        }
        else
        {
            return num.toString();
        }
    }

    public static Vector2 toVector2(this string s, char gap = '|')
    {
        var vs = s.toFloatList(gap);
        Vector2 vector2 = new Vector2();
        if (vs.Count == 2)
        {
            vector2.x = vs[0];
            vector2.y = vs[1];
        }
        else
        {
            debug.log("使用toVector2 参数错误", s);
        }

        return vector2;
    }

    public static string[] toSplit(this string s, char gap = '|')
    {
        string[] vs = s.Split(gap);
        if (vs == null || vs.Length <= 0)
        {
            vs = new string[1];
            vs[0] = s;
        }
        return vs;
    }

    public static List<string> toCopy(this string[] s, int str, int end)
    {
        List<string> vs = new List<string>();
        for (int i = str; i < end; i++)
        {
            vs.Add(s[i]);
        }
        return vs;
    }

    public static List<string> toCopy(this List<string> s, int str, int end)
    {
        List<string> vs = new List<string>();
        for (int i = str; i < end; i++)
        {
            vs.Add(s[i]);
        }
        return vs;
    }

    public static List<int> toIntList(this List<string> v)
    {
        List<int> rv = new List<int>();
        for (int i = 0; i < v.Count; i++)
        {
            rv.Add(v[i].toInt());
        }
        return rv;
    }

    public static List<int> toIntList(this string[] v)
    {
        List<int> rv = new List<int>();
        for (int i = 0; i < v.Length; i++)
        {
            rv.Add(v[i].toInt());
        }
        return rv;
    }

    public static List<float> toFloatList(this List<string> v)
    {
        List<float> rv = new List<float>();
        for (int i = 0; i < v.Count; i++)
        {
            rv.Add(v[i].toFloat());
        }
        return rv;
    }

    public static List<float> toFloatList(this string[] v)
    {
        List<float> rv = new List<float>();
        for (int i = 0; i < v.Length; i++)
        {
            rv.Add(v[i].toFloat());
        }
        return rv;
    }

    #region 组

    /// <summary>
    /// 字符串 list
    /// </summary>
    /// <param name="s"></param>
    /// <param name="gap"></param>
    /// <returns></returns>
    public static List<string> toStringList(this string s, char gap = '|')
    {
        return new List<string>(s.toSplit(gap));
    }

    /// <summary>
    /// 字符串矩阵list row行分割符号 pos位置分割符号
    /// </summary>
    /// <param name="s"></param>
    /// <param name="row"></param>
    /// <param name="pos"></param>
    /// <returns></returns>
    public static List<List<string>> toStringMatrixList(this string s, char row = '/', char pos = '|')
    {
        List<List<string>> Matrix = new List<List<string>>();
        string[] rowatt = s.toSplit(row);
        for (int i = 0; i < rowatt.Length; i++)
        {
            Matrix.Add(new List<string>(rowatt[i].toSplit(pos)));
        }
        return Matrix;
    }

    /// <summary>
    /// 字符串空间list axis1 z纬度, axis2 y纬度, axis3 x纬度
    /// </summary>
    /// <param name="s"></param>
    /// <param name="axis1"></param>
    /// <param name="axis2"></param>
    /// <param name="axis3"></param>
    /// <returns></returns>
    public static List<List<List<string>>> toStringSpaceList(this string s, char axis1 = '^', char axis2 = '/', char axis3 = '|')
    {
        List<List<List<string>>> Matrix = new List<List<List<string>>>();
        string[] xs = s.toSplit(axis1);
        for (int x = 0; x < xs.Length; x++)
        {
            List<List<string>> yl = new List<List<string>>();
            string[] ys = xs[x].toSplit(axis2);
            for (int y = 0; y < ys.Length; y++)
            {
                List<string> zl = new List<string>(ys[y].toSplit(axis3));
                yl.Add(zl);
            }
            Matrix.Add(yl);
        }
        return Matrix;
    }

    /// <summary>
    /// 字符串转intList
    /// </summary>
    public static List<int> toIntList(this string s, char gap = '|')
    {
        string[] satt = s.toSplit(gap);
        int item = 0;
        List<int> list = new List<int>();
        for (int i = 0; i < satt.Length; i++)
        {
            try
            {
                item = satt[i].toInt();
            }
            catch (System.Exception ex)
            {
                Debug.LogError(ex.ToString());
            }
            list.Add(item);
        }
        return list;
    }

    /// <summary>
    /// int矩阵 row行分割符号 pos位置分割符号
    /// </summary>
    /// <param name="s"></param>
    /// <param name="row"></param>
    /// <param name="pos"></param>
    /// <returns></returns>
    public static List<List<int>> toIntMatrixList(this string s, char row = '/', char pos = '|')
    {
        List<List<int>> Matrix = new List<List<int>>();
        string[] rowatt = s.toSplit(row);
        for (int j = 0; j < rowatt.Length; j++)
        {
            int item = 0;
            string[] satt = rowatt[j].toSplit(pos);
            List<int> list = new List<int>();
            for (int i = 0; i < satt.Length; i++)
            {
                try
                {
                    item = satt[i].toInt();
                }
                catch (System.Exception ex)
                {
                    Debug.LogError(ex.ToString());
                }
                list.Add(item);
            }
            Matrix.Add(list);
        }
        return Matrix;
    }

    /// <summary>
    /// Int空间list axis1 z纬度, axis2 y纬度, axis3 x纬度
    /// </summary>
    /// <param name="s"></param>
    /// <param name="axis1"></param>
    /// <param name="axis2"></param>
    /// <param name="axis3"></param>
    /// <returns></returns>
    public static List<List<List<int>>> toIntSpaceList(this string s, char axis1 = '^', char axis2 = '/', char axis3 = '|')
    {
        List<List<List<int>>> Matrix = new List<List<List<int>>>();
        string[] xs = s.toSplit(axis1);
        for (int x = 0; x < xs.Length; x++)
        {
            List<List<int>> yl = new List<List<int>>();
            string[] ys = xs[x].toSplit(axis2);
            for (int y = 0; y < ys.Length; y++)
            {
                List<int> zl = new List<int>();
                string[] zs = ys[y].toSplit(axis3);
                for (int z = 0; z < zs.Length; z++)
                {
                    int zv = zs[z].toInt();
                    zl.Add(zv);
                }
                yl.Add(zl);
            }
            Matrix.Add(yl);
        }
        return Matrix;
    }

    /// <summary>
    /// 字符串转FloatList
    /// </summary>
    public static List<float> toFloatList(this string s, char gap = '|')
    {
        string[] satt = s.toSplit(gap);
        float item = 0;
        List<float> list = new List<float>();
        for (int i = 0; i < satt.Length; i++)
        {
            try
            {
                item = satt[i].toFloat();
            }
            catch (System.Exception ex)
            {
                Debug.LogError(ex.ToString());
            }
            list.Add(item);
        }
        return list;
    }

    /// <summary>
    /// float矩阵 row行分割符号 pos位置分割符号
    /// </summary>
    /// <param name="s"></param>
    /// <param name="row"></param>
    /// <param name="pos"></param>
    /// <returns></returns>
    public static List<List<float>> toFloatMatrixList(this string s, char row = '/', char pos = '|')
    {
        List<List<float>> Matrix = new List<List<float>>();
        string[] rowatt = s.toSplit(row);
        for (int j = 0; j < rowatt.Length; j++)
        {
            float item = 0;
            string[] satt = rowatt[j].toSplit(pos);
            List<float> list = new List<float>();
            for (int i = 0; i < satt.Length; i++)
            {
                try
                {
                    item = satt[i].toFloat();
                }
                catch (System.Exception ex)
                {
                    Debug.LogError(ex.ToString());
                }
                list.Add(item);
            }
            Matrix.Add(list);
        }
        return Matrix;
    }

    /// <summary>
    /// float空间list axis1 z纬度, axis2 y纬度, axis3 x纬度
    /// </summary>
    /// <param name="s"></param>
    /// <param name="axis1"></param>
    /// <param name="axis2"></param>
    /// <param name="axis3"></param>
    /// <returns></returns>
    public static List<List<List<float>>> toFloatSpaceList(this string s, char axis1 = '^', char axis2 = '/', char axis3 = '|')
    {
        List<List<List<float>>> Matrix = new List<List<List<float>>>();
        string[] xs = s.toSplit(axis1);
        for (int x = 0; x < xs.Length; x++)
        {
            List<List<float>> yl = new List<List<float>>();
            string[] ys = xs[x].toSplit(axis2);
            for (int y = 0; y < ys.Length; y++)
            {
                List<float> zl = new List<float>();
                string[] zs = ys[y].toSplit(axis3);
                for (int z = 0; z < zs.Length; z++)
                {
                    float zv = zs[z].toFloat();
                    zl.Add(zv);
                }
                yl.Add(zl);
            }
            Matrix.Add(yl);
        }
        return Matrix;
    }

    #endregion 组

    /// <summary>
    /// 取其中的几位,并转换为整形
    /// </summary>
    /// <param name="s"></param>
    /// <param name="str">从0开始</param>
    /// <param name="len"></param>
    /// <returns></returns>
    public static int subToInt(this string s, int str, int len)
    {
        string z = s.Substring(str, len);
        return z.toInt();
    }

    private static bool isNumStringPool = false;
    private static Dictionary<int, string> NumStringPool = new Dictionary<int, string>();
    private static Dictionary<long, string> longStringPool = new Dictionary<long, string>();

    private static Dictionary<int, string> TemporaryNumStringPool = new Dictionary<int, string>();

    //适当的时候清空字符串缓存
    public static void ClearTemporaryNumStringPool()
    {
        TemporaryNumStringPool.Clear();
    }

    /// <summary>
    /// 常用数值缓存
    /// </summary>
    public static string toString(this int num)
    {
        if (!isNumStringPool)
        {
            isNumStringPool = true;
            for (int i = -150; i < 150; i++)
            {
                NumStringPool.Add(i, i.ToString());
            }
        }
        if (num < -150 || num >= 150)
        {
            string s;
            if (TemporaryNumStringPool.Count > 200)
            {
                TemporaryNumStringPool.Clear();
            }

            if (TemporaryNumStringPool.TryGetValue(num, out s))
            {
                return s;
            }
            else
            {
                s = num.ToString();
                TemporaryNumStringPool.Add(num, s);
                return s;
            }
        }
        else
        {
            return NumStringPool[num];
        }
    }

    public static string toString(this long num)
    {
        if (!longStringPool.ContainsKey(num))
        {
            longStringPool.Add(num, num.ToString());
        }
        return longStringPool[num];
    }

    /// <summary>
    /// 比较两个对象是否相等
    /// </summary>
    /// <param name="_str"></param>
    /// <param name="_str1"></param>
    /// <returns></returns>
    public static bool StringSame(this string _str, string _str1)
    {
        return string.Compare(_str, _str1, StringComparison.Ordinal) == 0;
    }

    #region 查询关键字方法

    private static int[] GetKmpNext(string pattern)
    {
        int[] next = new int[pattern.Length];
        next[0] = -1;
        if (pattern.Length < 2) return next;
        next[1] = 0;
        int i = 2, j = 0;
        while (i < pattern.Length)
        {
            if (pattern[i - 1] == pattern[j])
            {
                next[i++] = ++j;
            }
            else
            {
                j = next[j];
                if (j == -1)
                {
                    next[i++] = ++j;
                }
            }
        }
        return next;
    }

    /// <summary>
    /// 查询关键字 模糊查询 PC测试 "LevenshteinDis";{ "Ls", "La", "Ldd", "asde", "Lfasde", "in" } ,1000次 3毫秒
    /// </summary>
    /// <param name="source">原字符串</param>
    /// <param name="keywords">关键字列表用|分开</param>
    /// <returns>如果存在关键字返回true，反之返回false。</returns>
    public static bool to_find_Key(this string source, string[] keywords)
    {
        int wordCount = keywords.Length;
        int[][] nexts = new int[wordCount][];
        int i = 0;
        for (i = 0; i < wordCount; i++)
        {
            nexts[i] = GetKmpNext(keywords[i]);
        }
        i = 0;
        int[] j = new int[nexts.Length];
        while (i < source.Length)
        {
            for (int k = 0; k < wordCount; k++)
            {
                if (source[i] == keywords[k][j[k]])
                {
                    j[k]++;
                }
                else
                {
                    j[k] = nexts[k][j[k]];
                    if (j[k] == -1)
                    {
                        j[k]++;
                    }
                }
                if (j[k] >= keywords[k].Length)
                {
                    return true;
                }
            }
            i++;
        }
        return false;
    }

    #endregion 查询关键字方法

    #region 字符串匹配度查询

    /// <summary>
    /// 字符串匹配度查询 返回0-1的一个值,1表示完全匹配 PC测试 "LevenshteinDis"; "Levenshte";, 1000次 7毫秒
    /// </summary>
    /// <param name="source"></param>
    /// <param name="str2"></param>
    /// <returns></returns>
    public static float to_similarity(this string str1, string str2)
    {
        return LevenshteinDistance.Ins.LevenshteinDistancePercent(str1, str2);
    }

    /// <summary>
    /// 编辑距离算法
    /// </summary>
    private class LevenshteinDistance
    {
        private static LevenshteinDistance ins = null;

        public static LevenshteinDistance Ins
        {
            get
            {
                if (ins == null)
                {
                    return new LevenshteinDistance();
                }
                return ins;
            }
        }

        /// <summary>
        /// 取最小的一位数
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <param name="third"></param>
        /// <returns></returns>
        public int LowerOfThree(int first, int second, int third)
        {
            int min = first;
            if (second < min)
                min = second;
            if (third < min)
                min = third;
            return min;
        }

        public int Levenshtein_Distance(string str1, string str2)
        {
            int[,] Matrix;
            int n = str1.Length;
            int m = str2.Length;

            int temp = 0;
            char ch1;
            char ch2;
            int i = 0;
            int j = 0;
            if (n == 0)
            {
                return m;
            }
            if (m == 0)
            {
                return n;
            }
            Matrix = new int[n + 1, m + 1];

            for (i = 0; i <= n; i++)
            {
                //初始化第一列
                Matrix[i, 0] = i;
            }

            for (j = 0; j <= m; j++)
            {
                //初始化第一行
                Matrix[0, j] = j;
            }

            for (i = 1; i <= n; i++)
            {
                ch1 = str1[i - 1];
                for (j = 1; j <= m; j++)
                {
                    ch2 = str2[j - 1];
                    if (ch1.Equals(ch2))
                    {
                        temp = 0;
                    }
                    else
                    {
                        temp = 1;
                    }
                    Matrix[i, j] = LowerOfThree(Matrix[i - 1, j] + 1, Matrix[i, j - 1] + 1, Matrix[i - 1, j - 1] + temp);
                }
            }

            //for (i = 0; i <= n; i++)
            //{
            //    for (j = 0; j <= m; j++)
            //    {
            //        Console.Write(" {0} ", Matrix[i, j]);
            //    }
            //    Console.WriteLine("");
            //}
            return Matrix[n, m];
        }

        /// <summary>
        /// 计算字符串相似度
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public float LevenshteinDistancePercent(string str1, string str2)
        {
            float maxLenth = str1.Length > str2.Length ? str1.Length : str2.Length;
            float val = Levenshtein_Distance(str1, str2);
            return 1 - val / maxLenth;
        }
    }

    #endregion 字符串匹配度查询
}

public static class MathTool
{
    public static int toAbs(this int s)
    {
        return Mathf.Abs(s);
    }

    public static float toAbs(this float s)
    {
        return Mathf.Abs(s);
    }

    public static int toInt(this int s)
    {
        return s;
    }

    public static int toInt(this float s)
    {
        return (int)s;
    }

    public static long toLong(this float s)
    {
        return (long)s;
    }

    public static float toFloat(this int s)
    {
        return (float)s;
    }

    public static Vector3 toVector3(this Vector2 vector)
    {
        return new Vector3(vector.x, vector.y);
    }

    public static T toEnum<T>(this int eunm)
    {
        T t = default(T);
        try
        {
            t = (T)(Enum.ToObject(typeof(T), eunm));
        }
        catch (System.Exception)
        {
            debug.log("转枚举类似错误!!", eunm, typeof(T));
        }
        return t;
    }

    public static int toClamp(this int v, int min, int max)
    {
        return Mathf.Clamp(v, min, max);
    }

    public static float toClamp(this float v, float min, float max)
    {
        return Mathf.Clamp(v, min, max);
    }

    /// <summary>
    /// 概率幸运抽奖
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public static T toLucky<T>(this List<T> v)
    {
        T cur = default(T);
        if (v != null)
        {
            cur = v.ToArray().toLucky();
        }
        return cur;
    }

    /// <summary>
    /// 概率幸运抽奖
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public static T toLucky<T>(this T[] v)
    {
        T cur = default(T);
        if (v != null)
        {
            if (v.Length == 1)
            {
                cur = v[0];
            }
            else if (v.Length > 1)
            {
                int rand = UnityEngine.Random.Range(0, v.Length);
                cur = v[rand];
            }
        }
        return cur;
    }

    /// <summary>
    /// 区间随机 数组1位3=>3,或者2位,2位取两者的区间,3-4=>3.5
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public static float toExtent(this List<float> v)
    {
        float cur = -1;
        if (v != null)
        {
            if (v.Count == 1)
            {
                cur = v[0];
            }
            else if (v.Count == 2)
            {
                cur = UnityEngine.Random.Range(v[0], v[1]);
            }
        }
        return cur;
    }

    /// <summary>
    /// 最后一位
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="v"></param>
    /// <returns></returns>
    public static T toLastOneData<T>(this List<T> v)
    {
        T cur = default(T);
        if (v.Count >= 1)
        {
            return v[v.Count - 1];
        }
        return cur;
    }

    /// <summary>
    /// 判断两个点的距离小于某个值
    /// </summary>
    public static bool toPosLTDic(this Vector3 pos1, Vector3 pos2, float dic)
    {
        return dic * dic > (pos1 - pos2).sqrMagnitude;
    }

    /// <summary>
    /// 判断两个点的距离小于某个值
    /// </summary>
    public static bool toPosLTDic(this Vector2 pos1, Vector2 pos2, float dic)
    {
        return dic * dic > (pos1 - pos2).sqrMagnitude;
    }

    /// <summary>
    /// 保留几位小数
    /// </summary>
    public static float toRound(this float s, int num)
    {
        if (num > 0)
        {
            float b = (float)Mathf.Pow(10, num);
            float vs = s * b;
            long iv = (long)vs;
            s = (float)(iv / b);
        }
        return s;
    }

    /// <summary>
    /// 去除精度
    /// </summary>
    public static Vector3 toRemoveAccuracy(this Vector3 s)
    {
        s.x = s.x.toRound(2);
        s.y = s.y.toRound(2);
        s.z = s.z.toRound(2);
        return s;
    }

    public static Vector2 toVector2(this Vector3 s)
    {
        s.z = 0;
        return s;
    }

    /// <summary>
    /// 取其中的几位列如:123456.setSub(2,2);=&gt;34
    /// </summary>
    public static int sub(this int num, int str, int len)
    {
        if (len == 0)
        {
            return 0;
        }
        int newNum = num;
        int zlen = newNum.Length();
        int zend = zlen - str - len;
        zend = zend < 0 ? 0 : zend;
        if (zend > zlen)
        {
            debug.log("超过截取范围!!!!");
            return 0;
        }
        newNum /= (int)Math.Pow(10, zend);
        newNum %= (int)Math.Pow(10, len);
        return newNum;
    }

    /// <summary>
    /// 替换其中的几位,列如:123456.setSub(2,78);=&gt;127856
    /// </summary>
    public static int setSub(this int num, int str, int news)
    {
        int mlen = num.Length();
        int nlen = news.Length();
        int zend = mlen - str - nlen;
        zend = zend < 0 ? 0 : zend;
        int baseDigit = (int)Math.Pow(10, zend);
        int dislodge = num.sub(str, nlen) * baseDigit;//抹去位的值
        news *= baseDigit;//得到位

        num = num - dislodge + news;
        return num;
    }

    /// <summary>
    /// 获取一个整形的位长度
    /// </summary>
    public static int Length(this int num)
    {
        int zlen = 0;
        float u = num;
        for (int i = 0; i < 99; i++)
        {
            if (u < 1)
            {
                break;
            }
            u = u / 10;
            zlen++;
        }
        return zlen;
    }

    public static string RemoveCloneString(this string name)
    {
        bool isClone = true;
        int pos = name.IndexOf("(Clone)");

        if (pos <= 0)
        {
            pos = name.IndexOf("(");
            if (pos > 0)
            {
                isClone = false;
            }
        }

        if (pos > 0)
        {
            name = name.Substring(0, isClone ? pos : (pos - 1));
        }

        return name;
    }

    public static string RemoveBracket(this string _str)
    {
        if (_str.Contains("("))
        {
            var pos = _str.IndexOf("(");
            _str = _str.Substring(0, pos);
        }
        else if (_str.Contains("（"))
        {
            var pos = _str.IndexOf("（");
            _str = _str.Substring(0, pos);
        }

        return _str;
    }

    /// <summary>
    /// 在这个只区间内随机
    /// </summary>
    /// <param name="vector"></param>
    /// <returns></returns>
    public static Vector2 toRandom(this Vector2 vector)
    {
        Vector2 pos = new Vector2();
        pos.x = Random.Range(0, vector.x);
        pos.y = Random.Range(0, vector.y);
        return pos;
    }

    /// <summary>
    /// 范围内随机一个点
    /// </summary>
    /// <param name="box"></param>
    /// <returns></returns>
    public static Vector3 toRandomInPos(this BoxCollider2D box)
    {
        Vector3 pos = (box.size.toRandom() + box.offset).toVector3() + box.transform.position;
        return pos;
    }

    //public static List<Vector3> toRandomInPos(this BoxCollider2D box, float R, int Num)
    //{
    //    List<Vector3> vectors = new List<Vector3>();
    //    float rs = R / 2;
    //    int xl = (box.size.x / rs).toInt();
    //    int yl = (box.size.y / rs).toInt();
    //    for (int i = 0; i <= xl; i++)
    //    {
    //        for (int j = 0; j <= yl; j++)
    //        {
    //            Vector3 pos = new Vector3();
    //            pos.x = i * rs + box.offset.x - box.size.x / 2 + box.transform.position.x + Random.Range(-rs, rs);
    //            pos.y = j * rs + box.offset.y - box.size.y / 2 + box.transform.position.y + Random.Range(-rs, rs);
    //            vectors.Add(pos);
    //        }
    //    }
    //    vectors.RandomizeList();
    //    if (Num < vectors.Count)
    //    {
    //        vectors.RemoveRange(Num, vectors.Count - Num);
    //    }
    //    return vectors;
    //}

    /// <summary>
    /// 最大数量的内包含圆
    /// </summary>
    /// <param name="box"></param>
    /// <param name="R"></param>
    /// <returns></returns>
    public static int toMaxInCircle(this BoxCollider2D box, float R)
    {
        int MaxNum = (int)(box.size.x / R) * (int)(box.size.y / R);
        return MaxNum;
    }

    /// <summary>
    /// 判断一个多边形里面大楷有多少圆
    /// </summary>
    /// <param name="polygon"></param>
    /// <param name="R"></param>
    /// <returns></returns>
    public static int toMaxInCircle(this PolygonCollider2D polygon, float R)
    {
        int MaxNum = (polygon.toArea() / CircleArea(R)).toInt();
        MaxNum = MaxNum > 1 ? MaxNum - 1 : MaxNum;
        return MaxNum;
    }

    /// <summary>
    /// 矩形的面积
    /// </summary>
    /// <param name="box"></param>
    /// <returns></returns>
    public static float toArea(this BoxCollider2D box)
    {
        float Area = 0;
        Area = box.size.x * box.size.y;
        return Area;
    }

    /// <summary>
    /// 多边形的面积
    /// </summary>
    /// <param name="polygon"></param>
    /// <returns></returns>
    public static float toArea(this PolygonCollider2D polygon)
    {
        float Area = 0;
        for (int i = 0; i < polygon.pathCount; i++)
        {
            var pos = polygon.GetPath(i);
            Area += PolygonArea(pos);
        }
        return Area;
    }

    /// <summary>
    /// 计算多边形面积的函数
    /// (以原点为基准点,分割为多个三角形)
    /// 定理：任意多边形的面积可由任意一点与多边形上依次两点连线构成的三角形矢量面积求和得出。矢量面积=三角形两边矢量的叉乘。
    /// </summary>
    /// <param name="vectorPoints"></param>
    /// <returns></returns>
    public static float PolygonArea(Vector2[] vectorPoints)
    {
        int iCycle, iCount;
        iCycle = 0;
        float iArea = 0;
        iCount = vectorPoints.Length;

        for (iCycle = 0; iCycle < iCount; iCycle++)
        {
            iArea = iArea + (vectorPoints[iCycle].x * vectorPoints[(iCycle + 1) % iCount].y - vectorPoints[(iCycle + 1) % iCount].x * vectorPoints[iCycle].y);
        }

        return (float)Math.Abs(0.5 * iArea);
    }

    /// <summary>
    /// 计算圆的面积
    /// </summary>
    /// <param name="R"></param>
    /// <returns></returns>
    public static float CircleArea(float R)
    {
        return (float)(Math.PI * R * R);
    }

    /// <summary>
    /// 线性平滑到另外一个值
    /// </summary>
    public static float toLine(this float m, float v, float speed)
    {
        speed = Mathf.Abs(speed);
        float qv;
        if (m < v)
        {
            qv = v - speed;
            if (m > qv)
            {
                qv = m;
            }
        }
        else
        {
            qv = v + speed;
            if (m < qv)
            {
                qv = m;
            }
        }
        return qv;
    }

    /// <summary>
    /// 线性平滑到另外一个值
    /// </summary>
    public static Vector2 toLine(this Vector2 m, Vector2 v, float speed)
    {
        Vector2 dir = (v - m).normalized * speed;
        m.x = m.x.toLine(v.x, dir.x);
        m.y = m.y.toLine(v.y, dir.y);
        return m;
    }

    /// <summary>
    /// 线性平滑到另外一个值
    /// </summary>
    public static Vector3 toLine(this Vector3 m, Vector3 v, float speed)
    {
        Vector3 dir = (v - m).normalized * speed;
        m.x = m.x.toLine(v.x, dir.x);
        m.y = m.y.toLine(v.y, dir.y);
        m.z = m.y.toLine(v.z, dir.z);
        return m;
    }
}